package com.jeesite.modules.digitalize.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.entity.dto.system.reviewproces.DeleteIdListDTO;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.digitalize.entity.DigitalizePlan;
import com.jeesite.modules.digitalize.entity.DigitalizePlanDetail;
import com.jeesite.modules.digitalize.entity.dto.DigitalizePlanDetailAddDTO;
import com.jeesite.modules.digitalize.entity.dto.DigitalizePlanQuery;
import com.jeesite.modules.digitalize.entity.dto.DigitalizePlanTaskUpdateDTO;
import com.jeesite.modules.digitalize.service.DigitalizePlanDetailService;
import com.jeesite.modules.digitalize.service.DigitalizePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "数字-计划模块")
@RestController
@Validated
@RequestMapping("${adminPath}/digitalize/plan")
public class DigitalizePlanController {

    @Resource
    private DigitalizePlanDetailService digitalizePlanDetailService;

    @Resource
    private DigitalizePlanService digitalizePlanService;


    @ApiOperation("计划列表")
    @GetMapping("/pageList")
    public ResultVo<Page<DigitalizePlan>> selectAll(@Validated DigitalizePlanQuery dto) {
        Page<DigitalizePlan> page = new Page<DigitalizePlan>(dto.getPageNo(), dto.getPageSize());
        return ResultVo.success(this.digitalizePlanService.lambdaQuery().eq(DigitalizePlan::getProjectId, dto.getProjectId()).page(page));
    }


    @ApiOperation("删除计划")
    @PostMapping("/delete")
    public ResultVo deleteList(@RequestBody @Validated DeleteIdListDTO dto) {
        return this.digitalizePlanService.delete(dto.getIdList());
    }

    @ApiOperation("新增或保存计划")
    @PostMapping("/saveOrUpdate")
    public ResultVo<Boolean> saveOrUpdate(@Validated @RequestBody DigitalizePlan plan) {
        return ResultVo.success(this.digitalizePlanService.saveOrUpdate(plan));
    }

    @ApiOperation("导入计划")
    @ApiImplicitParam(name = "id", value = "计划id", required = true, dataType = "Long", paramType = "path")
    @PostMapping("/importPlan/{id}")
    public ResultVo<Object> importPlan(@PathVariable @NotNull(message = "计划id不能为空") Long id, MultipartFile file) {
        return digitalizePlanService.importPlan(id, file);
    }

    @ApiOperation("导出计划")
    @ApiImplicitParam(name = "id", value = "计划id", required = true, dataType = "Long", paramType = "path")
    @PostMapping("/exportPlan/{id}")
    public void exportPlan(@PathVariable @NotNull(message = "计划id不能为空") Long id, HttpServletResponse response) {
        digitalizePlanService.exportPlan(id, response);
    }

    @ApiOperation("新增计划任务")
    @PostMapping("/addPlanTask")
    public ResultVo<DigitalizePlanDetail> addPlanTask(@Validated @RequestBody DigitalizePlanDetailAddDTO planDetail) {
        return ResultVo.success(this.digitalizePlanDetailService.save(planDetail));
    }

    @ApiOperation("更新计划任务")
    @PostMapping("/updatePlanTask")
    public ResultVo<Boolean> updatePlanTask(@RequestBody DigitalizePlanDetail planDetail) {
        return ResultVo.success(this.digitalizePlanDetailService.updateById(planDetail));
    }

    @ApiOperation("删除计划任务")
    @ApiImplicitParam(name = "id", value = "计划任务id", required = true, dataType = "Long")
    @PostMapping("/deletePlanTask/{id}")
    public ResultVo<Boolean> deletePlanTask(@PathVariable Long id) {
        return ResultVo.success(this.digitalizePlanDetailService.deletePlanTask(id));
    }

    @ApiOperation("修改任务位置(上下左右移动)")
    @PostMapping("/updatePlanTaskSite")
    public ResultVo updatePlanTask(@RequestBody DigitalizePlanTaskUpdateDTO updateDTO) {
        return ResultVo.success(this.digitalizePlanDetailService.updatePlanTask(updateDTO));
    }

    @ApiOperation("计划详情列表- 按顺序排")
    @ApiImplicitParam(name = "planId", value = "计划id", required = true, dataType = "Long")
    @PostMapping("/planTaskList/{planId}")
    public ResultVo planTaskList(@PathVariable Long planId) {
        return ResultVo.success(this.digitalizePlanDetailService.getHierarchy(Constants.ROOT_ID, planId));
    }

    @ApiOperation("计划详情列表- 按父子级排")
    @ApiImplicitParam(name = "planId", value = "计划id", required = true, dataType = "Long")
    @PostMapping("/planTaskDeepList/{planId}")
    public ResultVo<List<DigitalizePlanDetail>> planTaskDeepList(@PathVariable Long planId) {
        return ResultVo.success(this.digitalizePlanDetailService.traverseDeep(planId));
    }

    @ApiOperation("计划详情列表-查询关联构建")
    @ApiImplicitParam(name = "id", value = "详情id", required = true, dataType = "Long")
    @PostMapping("/getComponentListByPlanDetailId/{id}")
    public ResultVo<String> getComponentListByPlanDetailId(@PathVariable Long id) {
        return ResultVo.success(this.digitalizePlanDetailService.getComponentListByPlanDetailId(id));
    }

}

